<?php
/**
 * 行政系统
 */
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\model\Papers;
use App\Http\Requests;
use App\model\Region;
use App\model\Car_adm;
use App\model\Car_attendance;
use App\model\Coach_type;
use App\model\Course;
use App\model\Service;
use App\model\User;
use App\model\Order;
use App\model\Student;
use App\model\Subject;
use App\model\Use_stu;
use App\model\Enroll;
use App\model\Complaints;
use App\model\Coach_group;
use App\model\Use_coach;
class ExecuController extends Controller
{   
    /**
     * 内容:个人中心
     */
    public function user()
    {
        return view('execu.user');
    }
    /**
     * 内容:个人信息
     */
    public function information()
    {
        return view('execu.information');
    }
    /**
    * 作者:*****
    * 内容:车辆设置(驾照设置)
    */
    public function vehicleSettings()
    {
        $model = new Papers();
        $arr = $model->paperslists();
        return view('execu.vehicleSettings',['arr'=>$arr]);
    }
    /**
     * 作者:*****
     * 内容:车辆登记
     */
    public function carRegister()
    {
        $model1 = new Papers();
        $model2 = new Coach_type();
        $model3 = new Car_attendance();
        $model4 = new Car_adm();
        $arr1 = $model1->paperslists();
        $arr2 = $model2->coachtype();
        $arr3 = $model3->car_attendance();
		$reg_arr = $model4->registration();
    	return view('execu.carRegister',['arr1'=>$arr1,'arr2'=>$arr2,'arr3'=>$arr3,'reg_arr'=>$reg_arr]);
    }
    /**
     * 内容:新增车辆
     * 作者:*****
     */
    public function newVehicle()
    {
        $papers = new Papers();
        $model = new Car_adm();
        $coachtype = new Coach_type();
        $carattendance = new Car_attendance();
        $arr1 = $papers->paperslists();
        $arr2 = $coachtype->coachtype();
        $reg_arr = $model->sitelists();
        $arr3 = $carattendance->car_attendance();
    	return view('execu.newVehicle',['arr1'=>$arr1,'arr2'=>$arr2,'arr3'=>$arr3,'reg_arr'=>$reg_arr]);
    }
    /**
     * 内容:车辆添加入库
     * 作者:*****
     */
	public function newvehicleadd()
	{	
		$arr['c_num'] = Input::get("c_num");
		$arr['c_license'] = Input::get("c_license");
		$arr['c_arctic'] = Input::get("c_arctic");
		$arr['car_id'] = Input::get("car_id");
		$arr['c_state'] = Input::get("c_state");
        $arr['c_time'] = Input::get("c_time");
		$arr['sit_ids'] = Input::get("sit_id");
        $arr['sit_id'] = json_encode($arr['sit_ids']);
        unset($arr['sit_ids']);
        $add = new Car_adm();
        $newadd = $add->newvehicleadd($arr);
        return view('/execu/carRegister');
	}
    /**
     * 作者:*****
     * 内容:车辆出勤
     */
    public function attendance()
    {   
        $data = date("Y-m-d");
        $rder = new Order();
        $arr = $rder->rders($data)->toArray();
        $car=Car_adm::select('c_num')->get()->toArray();
        foreach ($car as $k => $v)
		{
            foreach ($arr as $key => $val)
			{
                if($v['c_num']==$val['c_num'])
				{
                    $car[$k]['info']=$arr;
                }
				else
				{
                    $car[$k]['info']=array();
                }
            }
        }
    	return view('execu.attendance',['arr'=>$car]);
    }
    /**
     * 作者:*****
     * 内容:车辆维修
     */
    public function service()
    {	
		$ser = new Service();
		$arr = $ser->serviceLits();
    	return view('execu.service',['arr'=>$arr]);
    }

    /**
     * 内容:记录导出
     * 作者:*****
     */
    public function renexcelOut()
    {
        $register= new Car_adm();
        $arr=$register->registration();
        header('Content-type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=车辆登记表.xls');
        $str="序号\t车牌名\t车型\t行驶证号\t教练车型\t保险有效期\t登记日期\t状态\n";
        foreach($arr as $k=>$v)
		{
            $str.="$v->c_id\t$v->c_num\t$v->c_arctic\t$v->c_license\t$v->coa_coachtype\t$v->c_time\t$v->c_record\t$v->car_type\n";
        }
        return   iconv('utf-8','gbk',$str);
    }
    /**
     * 作者:*****
     * 内容:维修记录
     */
    public function serviceRecord()
    {	
		$ser = new Service();
		$arr = $ser->serviceLits();
    	return view('execu.serviceRecord',['arr'=>$arr]);
    }
    /**
    * 作者:***** 
    * 内容:维修记录导出
    */
    public function excel()
    {
        $ser = new Service();
        $arr = $ser->servicexls ();
        header('Content-type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=车辆维修记录表.xls');
        $str="序号\t报修人\t维修时间\t维修车辆\t保修原因\t修理人\t处理人\t维修结果\n";
        foreach($arr as $k=>$v)
		{
            $str.="$v->ser_id\t$v->use_name\t$v->ser_date\t$v->c_num\t$v->ser_cause\t$v->ser_cper\t$v->ser_per\t$v->ser_result\n";
        }
        return   iconv('utf-8','gbk',$str);
    }
	/**
	* 内容:车辆登记
	* 作者:*****
	*/
	public function regist(Request $request)
	{
		if($request->ajax())
        {
            $search = $request->all();
            foreach ($search as $key => $value)
			{
               if($value=='')
			   {
                    unset($search[$key]);
               }
            }
            $reg = new Car_adm();
            $regs = $reg->regist($search);
			
            if(empty($regs))
            {
                return view('errors.nodata');
            }
			else
			{
                return view('execu.carRegister_cn',compact('regs'));
            }
        }
	}
    /**
     * 作者:*****
     * 内容:车辆更换
     */
    public function renewal()
    {
    	return view('execu.renewal');
    }
    /**
     * 作者:*****
     * 内容:汽油添加
     */
    public function gasolineAddition()
    {
    	return view('execu.gasolineAddition');
    }
    /*
            * 作者: 李通
            * 内容：根据地区ID查询子分类
            */
    public function s_area()
    {
        $region = new Region();
        $p_id   = $_GET['p_id'];
        $arr    = $region->selectArea($p_id);
        if(empty($arr))
        {
            echo 0;
        }
        else
        {
            echo json_encode($arr);
        }
    }
    /*
     * 作者: 李通
     * 内容：学员报名
     * */
    public function apply(Request $request)
    {
        $enroll=new Enroll();
        $enroll_info=$enroll->selectEnroll();
        $page=$request->get('page');
        $page=isset($page)?$page:1;
        $user=new User();
        $num=$user->page($enroll_info,$page);
        return view('execu.apply',compact('enroll_info','num'));
    }
    /*
     * 作者: 李通
     * 内容：在校学员
     * */
    public function online(Request $request)
    {
        $user        = new User();
        $student     = new Student();
        $paper       = new Papers();
        $subject     = new Subject();
        $state       = new Use_stu();
        $page=$request->get('page');
        $c_page=isset($page)?$page:1;
        $s_data=$request->get('s_data');
        if($request->ajax())
        {
            $search      = $request->all();
            foreach($search as $k=>$v)
            {
                if(empty($v))
                {
                    unset($search[$k]);
                }
            }
            $online_info = $student->selectStudentInfo($search);
            if(empty($online_info))
            {
                return view('errors.nodata');
            }
            else
            {
                $s_data=serialize($search);
                $num=$user->page($online_info,$c_page);
                return view('execu.online_ch',compact('online_info','num','s_data'));
            }
        }elseif(!empty($s_data))
        {
            $sub_info    = $subject->selectSubject();
            $pap_info    = $paper->paperslists();
            $sta_info    = $state->selectState();
            $search=unserialize($s_data);
            $online_info = $student->selectStudentInfo($search);
            $num=$user->page($online_info,$c_page);
            return view('execu.online',compact('sub_info','pap_info','sta_info','online_info','num','s_data'));
        }
        else
        {
            $sub_info    = $subject->selectSubject();
            $pap_info    = $paper->paperslists();
            $sta_info    = $state->selectState();
            $online_info = $student->selectOnline();
            $num=$user->page($online_info,$c_page);
            return view('execu.online',compact('online_info','sub_info','pap_info','sta_info','num','s_data'));
        }
    }
    /*
     * 作者：李通
     * 内容：导出
     */
    public function excelOut(Request $request)
    {
        $search      = $request->all();
        $student     = new Student();
        $online_info = $student->selectStudentInfo($search);
        header('Content-type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=在校学员.xls');
        $str="序号\t姓名\t编号\t身份证号\t性别\t手机号\t车型\t学习阶段\t报名时间\t状态\n";
        foreach($online_info as $k=>$v){
            $str.="$k+1\t$v->use_name\t$v->use_num\t$v->use_card\t$v->use_sex\t$v->use_phone\t$v->pap_name\t$v->sub_name\t$v->use_enty\t$v->ust_state\n";
        }
        return   iconv('utf-8','gbk',$str);
    }
    /*
     * 作者：李通
     * 内容：修改学员信息页面
     */
    public function updateStuInfo(Request $request)
    {
        $id=$request->get('id');
        if(empty($id))
        {
            return view('errors.404');
        }
        else
        {
            $student  = new Student();
            $stu_info = $student->selectOneStudent($id);
            //var_dump($stu_info);die;
            if(empty($stu_info))
            {
                return view('errors.404');
            }
            else
            {
                $paper    = new Papers();
                $subject  = new Subject();
                $state    = new Use_stu();
                $sub_info = $subject->selectSubject();
                $pap_info = $paper->paperslists();
                $sta_info = $state->selectState();
                return view('execu.updateStu',compact('stu_info','sub_info','pap_info','sta_info'));
            }
        }
    }
    /*
     * 作者：李通
     * 内容：修改信息入库
     */
    public function updateInfoPro(Request $request)
    {
        $data = $request->all();
        $id   = $data['h_id'];
        $use_data['use_phone']     = isset($data['use_phone'])?trim($data['use_phone']):'';
        $use_data['use_num']       = isset($data['use_num'])?trim($data['use_num']):'';
        $use_data['use_name']      = isset($data['use_name'])?trim($data['use_name']):'';
        $use_data['use_sex']       = isset($data['use_sex'])?trim($data['use_sex']):'';
        $use_data['use_card']      = isset($data['use_card'])?trim($data['use_card']):'';
        $use_data['use_enty']      = isset($data['use_enty'])?trim($data['use_enty']):'';
        $stu_data['stu_entrytime'] = isset($data['use_enty'])?trim($data['use_enty']):'';
        $stu_data['per_id']        = isset($data['per_id'])?trim($data['per_id']):'';
        $stu_data['sub_id']        = isset($data['sub_id'])?trim($data['sub_id']):'';
        $stu_data['stu_state']     = isset($data['stu_state'])?trim($data['stu_state']):'';
        $user                      = new User();
        $student                   = new Student();
        $user    ->updateUserInfo($id,$use_data);
        $student ->updateStuInfo($id,$stu_data);
        return redirect('execu/online');
    }
    /*
     * 作者: 李通
     * 内容：入学登记
     */
    public function entrance(Request $request)
    {
        $region=new Region();
        if ($request->all()&&count($request->all())>1) {
            $data                   = $request->all();
            $arr['use_enty']        = isset($data['use_enty'])? $data['use_enty'] : '';
            $arr['use_num']         = isset($data['use_num']) ? $data['use_num'] : '';
            $arr['use_name']        = isset($data['use_name']) ? $data['use_name'] : '';
            $arr['use_nation']      = isset($data['use_nation']) ? $data['use_nation'] : '';
            $arr['use_sex']         = isset($data['use_sex']) ? $data['use_sex'] : '';
            $arr['use_card']        = isset($data['use_card']) ? $data['use_card'] : '';
            $arr['use_pwd']         = md5(substr($arr['use_card'], -6));
            $origin_pro             = isset($data['origin_pro']) ? $data['origin_pro'] : '';
            $origin_city            = isset($data['origin_city']) ? $data['origin_city'] : '';
            $arr['use_origin']      = $region->selectAreaname($origin_pro)['region_name'] . '省' . $region->selectAreaname($origin_city)['region_name'] . "市";
            $address_pro            = isset($data['address_pro']) ? $data['address_pro'] : '';
            $address_city           = isset($data['address_city']) ? $data['address_city'] : '';
            $address_dis            = isset($data['address_dis']) ? $data['address_dis'] : '';
            $address_info           = isset($data['address_info']) ? $data['address_info'] : '';
            $arr['use_address']     = $region->selectAreaname($address_pro)['region_name'] . '省' . $region->selectAreaname($address_city)['region_name'] . "市" . $region->selectAreaname($address_dis)['region_name'] . $address_info;
            $arr['use_phone']       = isset($data['use_phone']) ? $request->get('use_phone') : '';
            $arr['use_position']    ="学员";
            $arr['use_email']       = isset($data['use_email']) ? $request->get('use_email') : '';
            $photo                  = $_FILES['use_pic'];
            $caro_path              = "./images/" . md5($photo['name']) . time() . substr($photo['name'], strrpos($photo['name'], "."));
            if (move_uploaded_file($_FILES['use_pic']['tmp_name'], $caro_path)) {
                $arr['use_pic']     = $caro_path;
                $user = new User();
                if ($user->insert($arr)) {
                    $stu_info['stu_id']        = $user->selectID($arr['use_card'])['use_id'];
                    $stu_info['sub_id']        = 1;
                    $stu_info['cou_id']        = isset($data['cou_id']) ? $data['cou_id'] : '';
                    $stu_info['per_id']        = isset($data['per_id']) ? $data['per_id'] : '';
                    $stu_info['stu_entrytime'] = isset($data['use_enty'])? $data['use_enty'] : '';
                    $stu_info['stu_medical']   = isset($data['stu_medical']) ? $data['stu_medical'] : '';
                    $stu_info['stu_state']     = 1;
                    $student = new Student();
                    if ($student->insert($stu_info)) {
                        return redirect('/execu/online');
                    }
                }
            }

        }
        else
        {
            if(!empty($request->get('card')))
            {
                $card     = $request->get('card');
                $enroll   = new Enroll();
                $enr_info = $enroll->selectOneInfo($card);
            }
            $course       = new Course();
            $paper        = new Papers();
            $pap          = $paper->paperslists();
            $province     = $region->selectArea(1);
            $cou_name     = $course->selectCourse();
            return view('execu.entrance',compact('province','cou_name','pap','enr_info','use_num'));
            //var_dump($province);die;
        }
    }
    /*
    * 作者：李通
    * 内容：验证编号的唯一性
    */
    public function checkNum(Request $request)
    {
        $num        = trim($request->get('num'));
        $user       = new User();
        $num_info   = $user->selectNum($num);
        if($num_info)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
    }
    /*
    * 作者：李通
    * 内容：验证身份证号的唯一性
    */
    public function checkCard(Request $request)
    {
        $card       = trim($request->get('card'));
        $user       = new User();
        $card_info  = $user->selectCard($card);
        if($card_info)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
    }
    /*
     * 作者：李通
     * 内容：投诉管理
     */
    public function complaints(Request $request)
    {
        $user=new User();
        $page   = $request->get('page');
        $c_page=isset($page)?$page:1;
        $coa_name=$user->selectCoach();
        $complaints     = new Complaints();
        $id             = $request->get('id');
        if(!empty($id))
        {
            if($id=='all')
            {
                $com_info   = $complaints->selectComplaints();
            }
            else
            {
                $com_info   = $complaints->selectCoaComplaints($id);
            }
            if(empty($com_info))
            {
                return view('errors.nodata');
            }
            else
            {
                $num=$user->page($com_info,$c_page);
                if(!empty($id)&&isset($page))
                {
                    return view('execu.complaints',compact('com_info','id','coa_name','num'));
                }
                else
                {
                    return view('execu.complaints_ch',compact('com_info','id','num'));
                }
            }
        }
        else
        {
            $com_info=$complaints->selectComplaints();
            $num=$user->page($com_info,$c_page);
            return view('execu.complaints',compact('com_info','coa_name','id','num'));
        }
    }

    /*
     * 作者：李通
     * 内容：教练信息
     */
    public function coachinfo(Request $request)
    {
        $user = new User();
        $group=new Coach_group();
        $use_coach=new Use_coach();
        $page=$request->get('page');
        $c_page=isset($page)?$page:1;
        $s_data=$request->get('s_data');
        if($request->ajax())
        {
            $search=$request->all();
            foreach($search as $k=>$v)
            {
                if(empty($v))
                {
                    unset($search[$k]);
                }
            }
            $coa_info = $use_coach->searchCoachInfo($search);
            if(empty($coa_info))
            {
                return view('errors.nodata');
            }
            else {
                $s_data = serialize($search);
                $num = $user->page($coa_info, $c_page);
                return view('execu.coachinfo_ch', compact('coa_info','s_data','num'));
            }
        }
        elseif(!empty($s_data))
        {
            $search=unserialize($s_data);
            $gro_info=$group->selectGroup();
            $coa_info = $use_coach->searchCoachInfo($search);
            $num=$user->page($coa_info,$c_page);
            return view('execu.coachinfo',compact('gro_info','coa_info','num','s_data'));
        }
        else
        {
            $s_data="";
            $gro_info=$group->selectGroup();
            $coa_info=$use_coach->selectCoachInfo();
            $num=$user->page($coa_info,$c_page);
            return view('execu.coachinfo',compact('gro_info','coa_info','num','s_data'));
        }
    }
    /*
     * 作者：李通
     * 内容：教练分组模块
     */
    public function grouping()
    {
        return view('execu.grouping');
    }
    /*
     * 作者：李通
     * 内容：教练排课
     */
    public function arranging()
    {
        $order=new Order();
        $date=date("Y-m-d");
        $arr=$order->selectAllCourse($date);
        //var_dump($arr);die;
        $arr=json_encode($arr);
        $arr=json_decode($arr,true);
        foreach($arr as $k=>$v)
        {
            $arr[$k]['ca_state']=explode(',',$v['ca_state']);
            $use_id=explode(',',$v['ord_stu_id']);
            $name_data=array();
            foreach($use_id  as $key=>$val)
            {
                $user=new User();
                $use_name=$user->selectName($val);
                if(empty($use_name))
                {
                    $name=$val;
                }
                else
                {
                    $name=$use_name['use_name'];
                }
                $name_data[]=$name;
            }
            $arr[$k]['ord_stu_id']=$name_data;
        }
        //var_dump($arr);die;
        return view('execu.arranging',compact('arr'));
    }
}
